package com.ebt.m.proposal_v2.voice;

import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.text.TextUtils;

import com.ebt.m.proposal_v2.base.Constant;
import com.orhanobut.logger.Logger;

import java.io.File;
import java.io.IOException;

/**
 * @author archer.qi
 *         Created on 2017/5/19.
 */
public class TapApi {
    private static TapApi INSTANCE;

    public static TapApi getApi() {
        if (INSTANCE == null) {
            INSTANCE = new TapApi();
        }
        return INSTANCE;
    }

    private MediaRecorder mRecorder;
    private MediaPlayer mPlayer;

    /**
     * 开始录音
     */
    public void startRecord() {
        if (mRecorder == null) {
            mRecorder = new MediaRecorder();
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            createTargetWhenNotExists(Constant.outputTap);
            mRecorder.setOutputFile(Constant.outputTap);
        }
        try {
            mRecorder.prepare();
            mRecorder.start();
        } catch (IOException e) {
            e.printStackTrace();
            Logger.e("prepare() failed");
        }
    }

    /**
     * 停止录音
     */
    public void stopRecord() {
        if (mRecorder != null) {
            mRecorder.stop();
            mRecorder.release();
            mRecorder = null;
        }
    }

    public MediaRecorder getMediaRecoder() {
        return mRecorder;
    }

    /**
     * 播放
     *
     * @param fileName
     */
    public void playAudio(String fileName, MediaPlayer.OnCompletionListener listener) {
        if (mPlayer == null) {
            mPlayer = new MediaPlayer();
        }
        try {
            mPlayer.setDataSource(fileName);
            mPlayer.prepare();
            mPlayer.start();
            mPlayer.setOnCompletionListener(listener);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 停止播放
     */
    public void stopAudio() {
        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }

    /**
     * 获取音频文件时长
     *
     * @param fileName
     * @return
     */
    public long getAudioDuration(String fileName) {
        MediaPlayer player = new MediaPlayer();
        try {
            player.setDataSource(fileName);
            player.prepare();
            long duration = player.getDuration();
            player.release();
            return duration;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 检查录音文件是否创建；如果未创建，则新建
     *
     * @param path
     */
    public void createTargetWhenNotExists(String path) {
        if (TextUtils.isEmpty(path)) {
            return;
        }
        File file = new File(path);
        if (file.exists()) {
            return;
        }
        String dir = file.getParentFile().getPath();
        File dirFile = new File(dir);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
